#include "spo2_driver.h"
#include "gr_includes.h"
#include "app_uart.h"
#include "app_log.h"
#include "board_SK.h"
volatile uint8_t g_tdone = 0;
volatile uint8_t g_rdone = 2;
volatile uint16_t rlen = 0;
volatile uint16_t g_rx_buffer_len = 0;
void api_received_spo2_data(void);
uint8_t g_tx_buffer[UART_DATA_LEN] = {0};
uint8_t g_rx_buffer[UART_DATA_LEN] = {0};
unsigned char usart_rx_flag = 0;
unsigned char rx_buffer[30]={0};
unsigned char rx_buffer_len = 0;
unsigned char data_length=0;
unsigned char spo2_count = 0;

unsigned char usart_rec_over = 0;
uint8_t g_ring_buffer[200] = {0};
 unsigned char spo2_dis_buf[500];
unsigned char *spo2_head;
unsigned char *spo2_tail;
static unsigned char SPO2_DATA_UPLOAD_SCHED = 0;
static unsigned char SPO2_Wave_SendData_Buffer[30] = {0};
static unsigned char SPO2_SendData_Buffer[30] = {0};
static unsigned char spo2_0x81_count=0;
BYTEQ spo2_Q;
//spo2
static unsigned char PR_data = 255;
static unsigned char SPO2_data = 127;
static unsigned char PI_data = 0;
uint8_t *head_spo2=g_ring_buffer;
uint8_t *tail_spo2=g_ring_buffer;
extern uint8_t *head_spo2;
extern char spo2_update_flag;

uint8_t g_message_0[] = "APP UART example.\r\nPlease input characters(<126) and end with newline.\r\n";
uint8_t g_message_1[] = "Input:\r\n";
app_uart_params_t uart_params = {
    .id      = SPO2_UART_ID,
    .pin_cfg = {
           .tx = {
              .type = SPO2_UART_TX_IO_TYPE,
              .mux  = SPO2_UART_TX_PINMUX,
              .pin  = SPO2_UART_TX_PIN,
              .pull = APP_IO_PULLUP,
           },
           .rx = {
              .type = SPO2_UART_RX_IO_TYPE,
              .mux  = SPO2_UART_RX_PINMUX,
              .pin  = SPO2_UART_RX_PIN,
              .pull = APP_IO_PULLUP,
           },
    },

    .init = {
        .baud_rate = 115200,
        .data_bits = UART_DATABITS_8,
        .stop_bits = UART_STOPBITS_1,
        .parity    = UART_PARITY_NONE,
        .hw_flow_ctrl    = UART_HWCONTROL_NONE,
        .rx_timeout_mode = UART_RECEIVER_TIMEOUT_ENABLE,
    },
};
unsigned char get_PR_value(void)
{
    return PR_data;
}


unsigned char get_SpO2_value(void)
{
    return SPO2_data;
}


unsigned char get_PI_value(void)
{
    return PI_data;
}
void app_uart_callback(app_uart_evt_t *p_evt)
{
    if (p_evt->type == APP_UART_EVT_TX_CPLT)
    {
        g_tdone = 1;
    }
    if (p_evt->type == APP_UART_EVT_RX_DATA)
    {
        g_rdone = 1;
        g_rx_buffer_len = p_evt->data.size;
			  api_received_spo2_data();
       // memcpy(g_tx_buffer, g_rx_buffer, rlen);
//			 for(int i=0;i<UART_DATA_LEN;i++)
//			{
//					if(g_rx_buffer[i]==0x80)
//					{
//						i++;
//						for(int j=i;j<12+i;j++)
//						{
//							if(j>=UART_DATA_LEN)
//								break;
//							*head_spo2=g_rx_buffer[j];
//							head_spo2++;
//							if(head_spo2==&g_ring_buffer[199])
//								head_spo2=&g_ring_buffer[0];					
//						}			
//					}		
//			}	
			 app_uart_receive_async(SPO2_UART_ID, g_rx_buffer, sizeof(g_rx_buffer));
//			
			
    }
    if (p_evt->type == APP_UART_EVT_ERROR)
    {
        g_tdone = 1;
        g_rdone = 1;
    }
}
void api_received_spo2_data(void)
{
    memcpy(rx_buffer, g_rx_buffer, g_rx_buffer_len);
    rx_buffer_len = g_rx_buffer_len;
    usart_rec_over = 1;    
}
void spo2_uart_init(void)
{
    uint16_t ret = 0;
    app_uart_tx_buf_t uart_buffer = {0};

    uart_buffer.tx_buf = g_ring_buffer;
    uart_buffer.tx_buf_size = sizeof(g_ring_buffer);
    ret = app_uart_init(&uart_params, app_uart_callback, &uart_buffer);
    if (ret != APP_DRV_SUCCESS)
    {
        return;
    }
#if 0
    app_uart_transmit_sync(UART_ID, g_message_0, sizeof(g_message_0), 300);
    app_uart_transmit_sync(UART_ID, g_message_1, sizeof(g_message_1), 300);
#endif
  
}
void app_uart_demo(void)
{
    if(g_rdone > 0)
    {
        g_rdone = 0;
        app_uart_receive_async(SPO2_UART_ID, g_rx_buffer, sizeof(g_rx_buffer));
    } 
}
//cpu_init callback
void spo2_usart_init(void)
{
    spo2_uart_init(); 
	  app_uart_receive_async(SPO2_UART_ID, g_rx_buffer, sizeof(g_rx_buffer));
        spo2_head=&spo2_dis_buf[0];
    spo2_tail=&spo2_dis_buf[0];	
//    QInit(&spo2_Q, &spo2_dis_buf[0], 250);
    
    APP_LOG_INFO("spo2_usart_init");
}

void handle_spo2_data(void)
{
    unsigned char i = 0;
    if(usart_rec_over==1)
    {   
        usart_rec_over = 0;   
        //printf 
      //  APP_LOG_HEX_DUMP(rx_buffer,rx_buffer_len);  
       // printf("\r\nhandle_spo2_data %x\r\n",rx_buffer[0]);
       // print_hex_buf(rx_buffer,data_length);
 #if 1
        switch(rx_buffer[0])
        {
            case 0x80:  
            {
                for(i=0;i<10;i++)
                {
                    if(rx_buffer[i+1]>127)
                    {
                        rx_buffer[i+1]&=0x7f;
                    }
                    if((SPO2_data!=127))//figer on
                    {
                        //if(rec_spo2_cnt%2==0)//??2??
                        //QInPut(&spo2_Q, i);
											  if(i%2==0)
												{
											  *spo2_head=rx_buffer[i+1];
												spo2_head++;
											  if(spo2_head==&spo2_dis_buf[499])
													spo2_head=&spo2_dis_buf[0];
											}
													
    											
                    }
                    else 
                    {
											 if(i%2==0)
											 {
                       *spo2_head=80;
												spo2_head++;
											  if(spo2_head==&spo2_dis_buf[499])
													spo2_head=&spo2_dis_buf[0];
											}
                    }
                    //SPO2_SendData_Buffer[2+i] = rx_buffer[i+1];
                    //test
                    //SPO2_Wave_SendData_Buffer[1+i] = 128;
                    #if YK_BLE_PROTOCOL
                    SPO2_Wave_SendData_Buffer[1+i] = rx_buffer[i+1];
                    #else
                    SPO2_Wave_SendData_Buffer[3+i] = rx_buffer[i+1];
                    #endif
                }
                SPO2_DATA_UPLOAD_SCHED = 1;                                                                 
            };break;	
            
            case 0x81: //??'?????????z?
            {
                PR_data = rx_buffer[1];
                SPO2_data = rx_buffer[2];
                PI_data = rx_buffer[3];
							  spo2_update_flag=1;
                APP_LOG_INFO("PR_data:%d SPO2_data:%d PI_data:%d",PR_data,SPO2_data,PI_data);
            };break;	
            
            default:break;
        }
#endif				
    }
}
#if YK_BLE_PROTOCOL 
//main while callback
unsigned char  send_spo2_data_to_ble(void)
{
    unsigned char res = 0;
    unsigned char i = 0;
    
    
    //if((ECG_DATA_UPLOAD_SCHED == 1 )&&(patch_system_mode!=UPLOAD_FLASH_MODE))
    if(SPO2_DATA_UPLOAD_SCHED == 1 )
    {
        res = 1;
        SPO2_DATA_UPLOAD_SCHED = 0;
                
        //0x80 data
        SPO2_Wave_SendData_Buffer[0] = 0x80;  //??
        SPO2_Wave_SendData_Buffer[11] = 1;  //????
        SPO2_Wave_SendData_Buffer[12] = 0x00;
        SPO2_Wave_SendData_Buffer[13] = 0x00;
        SPO2_Wave_SendData_Buffer[14] = 0x00;
        
        //test
        SPO2_Wave_SendData_Buffer[15] = spo2_count++;
        //test
#if 0
        for(i=1;i<11;i++)
        {
            test_k++;
            if(test_k==30)
            {
                test_k = 0;
            }
            SPO2_Wave_SendData_Buffer[i] = test_k*3;
        }
#endif

//        delay_us(UART_WAITE_INTERVAL*1);
        //send_buf_to_ble(SPO2_Wave_SendData_Buffer, 16);
        send_buf_to_ble(SPO2_Wave_SendData_Buffer, 15);
        //APP_LOG_HEX_DUMP(SPO2_Wave_SendData_Buffer, 15);
//        delay_us(UART_WAITE_INTERVAL);
        
        //0x81 data
        spo2_0x81_count++;
        if(spo2_0x81_count>=5)
        {
            //spo2
            spo2_0x81_count = 0;
            SPO2_SendData_Buffer[0] = 0x81;  //??
            SPO2_SendData_Buffer[1] = SPO2_data;  //????
            SPO2_SendData_Buffer[2] = PR_data;  		//????
            SPO2_SendData_Buffer[3] = PI_data;  		//PI
            SPO2_SendData_Buffer[4] =0x00;
            SPO2_SendData_Buffer[5] =0x00;
            SPO2_SendData_Buffer[6] =PR_data/5;
            SPO2_SendData_Buffer[7] =0x00;
            SPO2_SendData_Buffer[8] =0x00;
            for(i=0;i<6;i++)
            {
                SPO2_SendData_Buffer[i+9] = 0x00;
            }
//            delay_us(UART_WAITE_INTERVAL);
            send_buf_to_ble(SPO2_SendData_Buffer, 15);
            //APP_LOG_HEX_DUMP(SPO2_SendData_Buffer, 15);
//            delay_us(UART_WAITE_INTERVAL);
            
        }
        
    }
	return res ;
}

#else

//main while callback
unsigned char  send_spo2_data_to_ble(void)
{
    unsigned char res = 0;
    unsigned char i = 0;
    
    if(SPO2_DATA_UPLOAD_SCHED == 1 )
    {
        res = 1;
        SPO2_DATA_UPLOAD_SCHED = 0;
                
        //0x80 data
        SPO2_Wave_SendData_Buffer[0] = 0xAA;  //??
        SPO2_Wave_SendData_Buffer[1] = 0x0E;  //????
        SPO2_Wave_SendData_Buffer[2] = 0x0F;
        SPO2_Wave_SendData_Buffer[13] = 0x00;
        SPO2_Wave_SendData_Buffer[14] = 0xCC;
        
        //test
        SPO2_Wave_SendData_Buffer[15] = spo2_count++;
        //test
#if 0
        for(i=1;i<11;i++)
        {
            test_k++;
            if(test_k==30)
            {
                test_k = 0;
            }
            SPO2_Wave_SendData_Buffer[i] = test_k*3;
        }
#endif

        //send_buf_to_ble(SPO2_Wave_SendData_Buffer, 16);
        send_buf_to_ble(SPO2_Wave_SendData_Buffer, 15);
        APP_LOG_HEX_DUMP(SPO2_Wave_SendData_Buffer, 15);
        
        //0x81 data
        spo2_0x81_count++;
        if(spo2_0x81_count>=5)
        {
            //spo2
            spo2_0x81_count = 0;
            i = 0;
            SPO2_SendData_Buffer[i++] = 0xAA;  //??
            SPO2_SendData_Buffer[i++] = 0x10;
            SPO2_SendData_Buffer[i++] = 0x0D;
            SPO2_SendData_Buffer[i++] = SPO2_data;  //????
            SPO2_SendData_Buffer[i++] = PR_data;  		//????
            SPO2_SendData_Buffer[i++] = PI_data;  		//PI
            SPO2_SendData_Buffer[i++] = 0x00; // RR ??
            SPO2_SendData_Buffer[i++] = 0x00; //byte5
            SPO2_SendData_Buffer[i++] = 0x00; //byte6
            SPO2_SendData_Buffer[i++] = 0x00; //byte7
            SPO2_SendData_Buffer[i++] = 0x00; //byte8
            SPO2_SendData_Buffer[i++] = 0x00;
            SPO2_SendData_Buffer[i++] = 0xCC; 
            send_buf_to_ble(SPO2_SendData_Buffer, 13);
            APP_LOG_HEX_DUMP(SPO2_SendData_Buffer, 13);            
        }
        
    }
	return res ;
}
#endif